package com.tjetc.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

import jakarta.servlet.ServletContext;

// 上下文工具类
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
 private static ApplicationContext applicationContext;

 @Override
 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextProvider.applicationContext = applicationContext;
        System.out.println("ApplicationContext已设置: " + applicationContext);
        System.out.println("ApplicationContext类型: " + applicationContext.getClass().getName());
 }

 public static ApplicationContext getApplicationContext() {
         return applicationContext;
 }

 public static ApplicationContext getApplicationContext(ServletContext servletContext) {
 if (applicationContext == null) {
         applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
         System.out.println("从ServletContext获取ApplicationContext: " + applicationContext);
 }
        return applicationContext;
}
} 